﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Axa.CasinoApp.Client.ProcessLayer.WebRequest;
using Axa.CasinoApp.Client.ProcessLayer.Dto;
using System.Collections.Generic;

namespace Axa.CasinoApp.Client.ProcessLayer.UseCases
{
    public class GameUseCase : IGameUseCase
    {
        private string _url;

        public event EventHandler InitializeGameRequestCompleted;
        public event EventHandler<GetPotResponseEventArgs> GetPotRequestCompleted;

        public GameUseCase(string url)
        {
            _url = url;
        }

        public void InitializeGameAsync(int numberOfPlayers)
        {
            string envelope =string.Format(
                              @"<?xml version=""1.0"" encoding=""utf-8""?>
                                <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:cas=""http://casino.axis.ws.com"">
                                    <soapenv:Header/>
                                    <soapenv:Body>
                                        <cas:initializeGame>
                                            <cas:anAmountOfPlayers>{0}</cas:anAmountOfPlayers>
                                        </cas:initializeGame>
                                    </soapenv:Body>
                                </soapenv:Envelope>"
                              , numberOfPlayers);

            AsyncWebRequestHandler requestHandler = new AsyncWebRequestHandler(envelope);
            requestHandler.RequestCompleted += new EventHandler<ResponseEventArgs>(InitializeGameRequestCompletedHandler);
            requestHandler.BeginRequestAsync(_url);
        }

        private void InitializeGameRequestCompletedHandler(object sender, ResponseEventArgs e)
        {
            OnInitializeGameRequestCompleted();
        }

        public void GetPotAsync()
        {
            string envelope = @"<?xml version=""1.0"" encoding=""utf-8""?>
                                <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:cas=""http://casino.axis.ws.com"">
                                   <soapenv:Header/>
                                   <soapenv:Body>
                                      <cas:getPot/>
                                   </soapenv:Body>
                                </soapenv:Envelope>";
            AsyncWebRequestHandler requestHandler = new AsyncWebRequestHandler(envelope);
            requestHandler.RequestCompleted += new EventHandler<ResponseEventArgs>(GetPotRequestCompletedHandler);
            requestHandler.BeginRequestAsync(_url);
        }

        private void GetPotRequestCompletedHandler(object sender, ResponseEventArgs e)
        {
            //Hack 2371, nach Elementen suchen tutet irgendwie nicht, darum so durch die Nodes gehen.
            //Liebe Kinder: Bitte dies zu hause nicht nachmachen! ;)
            XElement xelmn = XElement.Parse(e.ResponseContent);
            var row1 = (((xelmn.FirstNode as XElement).FirstNode as XElement).FirstNode as XElement).FirstNode;
            var row2 = row1.NextNode;
            var row3 = row2.NextNode;
            var playerWon = row3.NextNode;

            GetPotResponse getPotResponse =
                new GetPotResponse()
                 {
                     NumberRow1 = Convert.ToInt32((row1 as XElement).Value),
                     NumberRow2 = Convert.ToInt32((row2 as XElement).Value),
                     NumberRow3 = Convert.ToInt32((row3 as XElement).Value),
                     HasPlayerWon = Convert.ToBoolean((playerWon as XElement).Value)
                 };

            OnGetPotRequestCompleted(new GetPotResponseEventArgs(getPotResponse));
        }

        private void OnInitializeGameRequestCompleted()
        {
            EventHandler handler = InitializeGameRequestCompleted;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        private void OnGetPotRequestCompleted(GetPotResponseEventArgs e)
        {
            EventHandler<GetPotResponseEventArgs> handler = GetPotRequestCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
}
